﻿using System;
using System.Web.UI;
using System.Windows;

namespace com.orange.examination
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IdentifyingCode.Text = RandomBuild.Random();//产生验证码
            PostBack();//页面刷效果
            ManagerComponentVisible();//控件根据不同的情况，判断是否展示出来

        }

        protected void Register_Click(object sender, EventArgs e)
        {

            if (txtName.Text == "" || txtNum.Text == "" || txtPwd.Text == "" || txtPwd2.Text == "")//判断信息表是否填写完整
            {
               
                Response.Write("<script>window.alert('请将全部信息填写完整！');</script>");

            }
            else
            {
               if (txtCode.Text.Trim().ToString() != IdentifyingCode.Text.Trim().ToString())//判断验证码是否正确
                {
                    Response.Write("<script>window.alert('验证码错误！');</script>");
                }
                else
                {
                    if (this.ddlstatus.SelectedValue == "学生")//判断是否是学生
                    {
                        //判断数据库中是否已经存在该学号的学生，存在则不能再次注册
                        if (CheckExist.CheckStudentExist(txtNum.Text))
                        {                           
                            Response.Write("<script>window.alert('该学号已经注册过了，请返回登入页面登入！');</script>");
                        }//不存在则可以注册，把数据写入数据库
                        else
                        {
                            int i=BaseClass.OperateData("insert into tb_student (StudentName,StudentNum,StudentPwd,StudentSex) values('" + txtName.Text.Trim() + "','" + txtNum.Text.Trim() + "','" + txtPwd.Text.Trim() + "','" + txtSexOrCourse.Text.Trim() + "')");
                            MessageBOX(i);//数据库写入是否成功，弹窗提示
                        }

                    }

                    if (this.ddlstatus.SelectedValue == "教师")//判断注册的是教师
                    {
                        //判断数据库中是否已经存在该工号的教师，存在则不能再次注册
                        if (CheckExist.CheckTeacherExist(txtNum.Text))
                        {
                            Response.Write("<script>window.alert('该工号已经注册过了，请返回登入页面登入！');</script>");
                        }//不存在则可以注册，把数据写入数据库
                        else
                        {
                            int i = BaseClass.OperateData("insert into tb_teacher (TeacherName,TeacherNum,TeacherPwd,TeacherCourse) values('" + txtName.Text.Trim()+ "','" + txtNum.Text.Trim() + "','" + txtPwd.Text.Trim() + "','" + txtSexOrCourse.Text.Trim() + "')");
                            MessageBOX(i);//数据库写入是否成功，弹窗提示
                        }

                    }
                }

            }

               
        }

        protected void ReturnLogin_Click(object sender, EventArgs e)//返回到登入页面
        {
            Response.Redirect("Login.aspx");
        }
        protected void PostBack()//刷新页面时要做的事
        {
            if (Page.IsPostBack)
            {
                //确保更新页面时密码不被删除
                this.txtPwd.Attributes.Add("value", this.txtPwd.Text);
                this.txtPwd2.Attributes.Add("value", this.txtPwd2.Text);
                this.SelectSex.Attributes.Add("value", this.SelectSex.SelectedValue);
            }
            else { }
        }

        protected void ManagerComponentVisible()//管理控件的显示与否
        {
            if (this.ddlstatus.SelectedValue == "学生")
            {
                Course.Visible = false;
                Sex.Visible = true;

                TeacherID.Visible = false;
                StudentID.Visible = true;

                txtSexOrCourse.Visible = false;
                SelectSex.Visible = true;
            }
            else
            {
                Course.Visible = true;
                Sex.Visible = false;

                TeacherID.Visible = true;
                StudentID.Visible = false;

                txtSexOrCourse.Visible = true;
                SelectSex.Visible = false;
            }
        }

        protected void MessageBOX(int i) //数据库写入时，成功与否的弹窗
        {
            if (i > 0)
            {
                Response.Write("<script>window.alert('注册成功，请返回登入页面登入！');</script>");
            }
            else
            {
                Response.Write("<script>window.alert('注册失败，请重试！');</script>");
            }
        }

    }
}